﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//Import Section
using System.IO;
using System.Net;
using Microsoft.VisualBasic;

namespace WindowsFormsApplication1
{
    public partial class frmMain : Form
    {
        //Database
        ADODB.Connection con;
        ADODB.Recordset rs;
        ADODB.Recordset rs2;
        double recCount;
        int tblCount;
        object oMissing;

        //Distance
        WebRequest request;
        HttpWebResponse response;
        Stream dataStream;
        StreamReader reader;

        //Probability and Mode
        double probBusTo = 0;
        double probWalkTo = 0;
        double probAutoTo = 0;
        double probBusFr = 0;
        double probWalkFr = 0;
        double probAutoFr = 0;


        public frmMain()
        {
            con = new ADODB.Connection();
            rs = new ADODB.Recordset();
            rs2 = new ADODB.Recordset();
            oMissing = System.Reflection.Missing.Value;
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            Reset();
        }

        private void btnFile_Click(object sender, EventArgs e)
        {
            FileDialog.ShowDialog();
        }

        private void FileDialog_FileOk(object sender, CancelEventArgs e)
        {
            Reset();
            txtFile.Text = FileDialog.FileName;
            if (txtFile.Text.Length > 0)
            {
                lblFile.Visible = true;
                lblFile.Text = "Loading...";
                //Database
                try
                {
                    lblFile.Text = "Connecting...";
                    con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtFile.Text, null, null, 0);
                    rs = con.OpenSchema(ADODB.SchemaEnum.adSchemaTables, oMissing, oMissing);
                    if (!rs.BOF) rs.MoveFirst();
                    tblCount = 0;
                    while (!rs.EOF)
                    {
                        if ((string)rs.Fields["TABLE_TYPE"].Value == "TABLE") tblCount += 1;
                        rs.MoveNext();
                    }
                    if (!rs.BOF) rs.MovePrevious();
                    if (!(tblCount >= 6))
                    {
                        //Interaction.MsgBox("Incorrect Database Selected!!", MsgBoxStyle.Critical, "STS - Error");
                        MessageBox.Show("Incorrect Database Selected!!", "STS - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        if (con.State == 1) con.Close();
                        if (rs.State == 1) rs.Close();
                        lblFile.Text = "Error Connecting to Database!! Select Correct Database";
                        return;
                    }
                    if (rs.State == 1) rs.Close();
                }
                catch (Exception ex)
                {
                    lblFile.Text = "Error Connecting to database!!";
                    MessageBox.Show(ex.Message, "STS - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                //MessageBox.Show("Connection done", "STS - Error", MessageBoxButtons.OK);
                //Tables
                try
                {
                    lblFile.Text = "Retrieving required tables...";
                    rs.Open("Select count(*) from Children", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                    recCount = Convert.ToDouble(rs.Fields[0].Value);
                    if (rs.State == 1) rs.Close();
                    rs.Open("SELECT * FROM Children C, School S WHERE C.SID = S.ID", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                    if (!rs.BOF) rs.MoveFirst();

                    //if (rs2.State == 1) rs2.Close();
                    //rs2.Open("DELETE * FROM ChildTravel", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                    if (rs2.State == 1) rs2.Close();
                    rs2.Open("SELECT * FROM ChildTravel", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                    if (!rs2.BOF) rs2.MoveFirst();

                    btnCompute.Enabled = true;
                    lblFile.Text = "Data Loading Done!  Click Run Model to proceed...";
                }
                catch (Exception ex)
                {
                    lblFile.Text = "Error Retrieving Tables!!";
                    Interaction.MsgBox(ex.Message, MsgBoxStyle.Critical, "STS - Error");
                    //MessageBox.Show(ex.Message, "STS - Error", MessageBoxButtons.OK);
                }
            }
        }

        private void lblFile_Click(object sender, EventArgs e)
        {
            string temp = null;
            double tmpDouble = 0;

            if (rs.State == 1) rs.Close();
            rs.Open("Select ToMode, COUNT(ToMode) from ChildTravel GROUP BY ToMode", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
            if (!rs.BOF) rs.MoveFirst();
            temp = "Share of each mode:" + Constants.vbCr + "To School - ";
            while (!rs.EOF)
            {
                tmpDouble = Convert.ToDouble(rs.Fields[1].Value) / recCount * 100;
                if (Convert.ToInt32(rs.Fields[0].Value) == 1) temp += Constants.vbCr + "School Bus : " + tmpDouble.ToString("N2") + "%";
                if (Convert.ToInt32(rs.Fields[0].Value) == 2) temp += Constants.vbCr + "Walk/Bike : " + tmpDouble.ToString("N2") + "%";
                if (Convert.ToInt32(rs.Fields[0].Value) == 3) temp += Constants.vbCr + "Car : " + tmpDouble.ToString("N2") + "%";
                rs.MoveNext();
            }

            if (rs2.State == 1) rs2.Close();
            rs2.Open("Select FrMode, COUNT(FrMode) from ChildTravel GROUP BY FrMode", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
            if (!rs2.BOF) rs.MoveFirst();
            temp += Constants.vbCr + Constants.vbCr + "From School - ";
            while (!rs2.EOF)
            {
                tmpDouble = Convert.ToDouble(rs2.Fields[1].Value) / recCount * 100;
                if (Convert.ToInt32(rs2.Fields[0].Value) == 1) temp += Constants.vbCr + "School Bus : " + tmpDouble.ToString("N2") + "%";
                if (Convert.ToInt32(rs2.Fields[0].Value) == 2) temp += Constants.vbCr + "Walk/Bike : " + tmpDouble.ToString("N2") + "%";
                if (Convert.ToInt32(rs2.Fields[0].Value) == 3) temp += Constants.vbCr + "Car : " + tmpDouble.ToString("N2") + "%";
                rs2.MoveNext();
            }

            if (rs.State == 1) rs.Close();
            if (rs2.State == 1) rs2.Close();
            Interaction.MsgBox(temp, MsgBoxStyle.OkOnly, "STS-Summary");
        }

        private void btnCompute_Click(object sender, EventArgs e)
        {
            string chldAddr = null;
            string schAddr = null;
            double temp = 0;
            TimeSpan elapsed = default(TimeSpan);
            object[] fieldsArray = new object[10];
            object[] values = new object[10];


            txtFile.Enabled = false;
            btnFile.Enabled = false;
            btnCompute.Enabled = false;
            lblFile.Visible = true;

            fieldsArray[0] = "ID";
            fieldsArray[1] = "ToMode";
            fieldsArray[2] = "FrMode";
            fieldsArray[3] = "Distance";
            fieldsArray[4] = "ProbToBus";
            fieldsArray[5] = "ProbToWalk";
            fieldsArray[6] = "ProbToAuto";
            fieldsArray[7] = "ProbFrBus";
            fieldsArray[8] = "ProbFrWalk";
            fieldsArray[9] = "ProbFrAuto";

            if (!rs.BOF) rs.MoveFirst();
            if (!rs2.EOF) rs2.MoveLast();
            pgbModel.Visible = true;
            pgbModel.Value = 0;
            pgbModel.Increment(10);
            lblFile.Text = "Calculating... 1 out of " + recCount + " records";
            static_btnCompute_Click_start = DateTime.Now;
            Update();
            for (int i = 1; i <= recCount; i++)
            {
                //Distance Calculation
                chldAddr = Convert.ToString(rs.Fields["StName"].Value) + " " + Convert.ToString(rs.Fields["StTag"].Value) + " " + Convert.ToString(rs.Fields["StDir"].Value) + " " + Convert.ToString(rs.Fields["C.City"].Value) + " " + Convert.ToString(rs.Fields["C.State"].Value) + " " + Convert.ToString(rs.Fields["C.Zip"].Value);
                schAddr = Convert.ToString(rs.Fields["Address"].Value) + " " + Convert.ToString(rs.Fields["S.City"].Value) + " " + Convert.ToString(rs.Fields["S.State"].Value) + " " + Convert.ToString(rs.Fields["S.Zip"].Value);
                temp = getDistance(chldAddr, schAddr);

                //Setting Up Values
                values[0] = rs.Fields["C.ID"].Value;
                values[1] = getToMode(temp, Convert.ToInt32(rs.Fields["Type"].Value), Convert.ToInt32(rs.Fields["Grade"].Value), Convert.ToInt32(rs.Fields["RID"].Value));
                values[2] = getFrMode(temp, Convert.ToInt32(rs.Fields["Type"].Value), Convert.ToInt32(rs.Fields["Grade"].Value), Convert.ToInt32(rs.Fields["RID"].Value));
                values[3] = temp;
                values[4] = probBusTo;
                values[5] = probWalkTo;
                values[6] = probAutoTo;
                values[7] = probBusFr;
                values[8] = probWalkFr;
                values[9] = probAutoFr;

                try
                {
                    rs2.AddNew(fieldsArray, values);
                }
                catch (Exception ex)
                {
                    Interaction.MsgBox(ex.Message, MsgBoxStyle.Critical, "STS - Error");
                    System.Environment.Exit(0);
                }

                if (!rs.EOF) rs.MoveNext();
                chldAddr = " ";
                schAddr = " ";

                //Resetting values
                temp = -1;
                probBusTo = -1;
                probWalkTo = -1;
                probAutoTo = -1;
                probBusFr = -1;
                probWalkFr = -1;
                probAutoFr = -1;

                if (i % (recCount / 10) == 0)
                {
                    lblFile.Text = "Calculating... " + i + " out of " + recCount + " records";
                    pgbModel.Increment(10);
                    Update();
                }
            }
            rs2.Update(oMissing, oMissing);

            if (rs.State == 1) rs.Close();
            if (rs2.State == 1) rs2.Close();
            static_btnCompute_Click_finish = DateTime.Now;
            elapsed = static_btnCompute_Click_finish.Subtract(static_btnCompute_Click_start);
            lblFile.Text = "Model Completed in " + elapsed.TotalSeconds.ToString("N2") + " seconds! Click here for summary!";
            lblFile.Enabled = true;
            txtFile.Enabled = true;
            btnFile.Enabled = true;
        }
        static DateTime static_btnCompute_Click_finish = default(DateTime);
        static DateTime static_btnCompute_Click_start = default(DateTime);

        private double getDistance(string frAddr, string toAddr)
        {
            request = WebRequest.Create("http://maps.google.com/maps?saddr=" + frAddr + "&daddr=" + toAddr + "&output=dragdir");
            response = (HttpWebResponse)request.GetResponse();
            if (response.StatusDescription == "OK")
            {
                dataStream = response.GetResponseStream();
                reader = new StreamReader(dataStream);
                string[] split = reader.ReadToEnd().Split(new Char[] { '(', '\\', 'x' }, 3);
                reader.Close();
                dataStream.Close();
                response.Close();
                return Convert.ToDouble(split[1]);
            }
            else
            {
                return -1;
            }
        }

        private void Reset()
        {
            lblFile.Text = "";
            lblFile.Visible = false;
            lblFile.Enabled = false;
            txtFile.Text = "";
            txtFile.Enabled = true;
            btnFile.Enabled = true;
            pgbModel.Visible = false;
            btnCompute.Enabled = false;
            if (rs.State == 1) rs.Close();
            if (rs2.State == 1) rs2.Close();
            if (con.State == 1) con.Close();
            probBusTo = -1;
            probWalkTo = -1;
            probAutoTo = -1;
            probBusFr = -1;
            probWalkFr = -1;
            probAutoFr = -1;
        }

        private object getToMode(double distance, int type, int grade, int race)
        {
            double toBus = 0;
            double toWalk = 0;
            Random rand = new Random();

            toBus = -1.269 - 0.195;
            toWalk = -1.269 - 0.195;

            //Distance
            if (distance > 0.25 & distance <= 0.5)
            {
                toBus += 0.379;
                toWalk += 0.339;
            }
            if (distance > 0.5 & distance <= 0.75)
            {
                toBus += 0.292;
                toWalk -= 1.828;
            }
            if (distance > 0.75 & distance <= 1.0)
            {
                toBus += 1.729;
                toWalk -= 1.765;
            }
            if (distance > 1.0 & distance <= 1.5)
            {
                toBus += 1.851;
                toWalk -= 3.272;
            }
            if (distance > 1.5 & distance <= 2.0)
            {
                toBus += 2.465;
                toWalk -= 4.304;
            }
            if (distance > 2.0 & distance <= 3.0)
            {
                toBus += 2.279;
                toWalk -= 5.601;
            }
            if (distance > 3.0)
            {
                toBus += 2.741;
                toWalk -= 4.268;
            }

            //Type
            if (type == 1)
            {
                toBus += 0.939;
                toWalk -= 0.022;
            }

            //Grade
            if (grade == 1)
            {
                toBus += 0.33;
                toWalk += 0.32;
            }
            if (grade == 2)
            {
                toBus -= 0.19;
                toWalk += 0.339;
            }
            if (grade == 3)
            {
                toBus += 0.568;
                toWalk -= 0.776;
            }
            if (grade == 4)
            {
                toBus += 0.233;
                toWalk += 0.377;
            }
            if (grade == 5)
            {
                toBus += 0.757;
                toWalk += 0.71;
            }
            if (grade == 6)
            {
                toBus += 1.662;
                toWalk += 1.663;
            }

            //Race
            if (race == 1)
            {
                toBus -= 1.145;
                toWalk += 0.052;
            }

            toBus = Math.Exp(toBus);
            toWalk = Math.Exp(toWalk);

            probBusTo = toBus / (1 + toBus + toWalk);
            probWalkTo = toWalk / (1 + toBus + toWalk);
            probAutoTo = 1 / (1 + toBus + toWalk);

            double i = 0;
            i = rand.NextDouble();
            if ((i <= probBusTo))
            {
                //School Bus
                return 1;
            }
            else if ((i <= (probBusTo + probWalkTo)))
            {
                //Walk or Bike
                return 2;
            }
            else
            {
                //car
                return 3;
            }
        }

        private object getFrMode(double distance, int type, int grade, int race)
        {
            double frBus = 0;
            double frWalk = 0;
            Random rand = new Random();

            frBus = -1.269;
            frWalk = -1.269;

            //Distance
            if (distance > 0.25 && distance <= 0.5)
            {
                frBus += 0.379;
                frWalk += 0.339;
            }
            if (distance > 0.5 && distance <= 0.75)
            {
                frBus += 0.292;
                frWalk -= 1.828;
            }
            if (distance > 0.75 && distance <= 1.0)
            {
                frBus += 1.729;
                frWalk -= 1.765;
            }
            if (distance > 1.0 && distance <= 1.5)
            {
                frBus += 1.851;
                frWalk -= 3.272;
            }
            if (distance > 1.5 && distance <= 2.0)
            {
                frBus += 2.465;
                frWalk -= 4.304;
            }
            if (distance > 2.0 && distance <= 3.0)
            {
                frBus += 2.279;
                frWalk -= 5.601;
            }
            if (distance > 3.0)
            {
                frBus += 2.741;
                frWalk -= 4.268;
            }

            //Type
            if (type == 1)
            {
                frBus += 0.939;
                frWalk -= 0.022;
            }

            //Grade
            if (grade == 1)
            {
                frBus += 0.33;
                frWalk += 0.32;
            }
            if (grade == 2)
            {
                frBus -= 0.19;
                frWalk += 0.339;
            }
            if (grade == 3)
            {
                frBus += 0.568;
                frWalk -= 0.776;
            }
            if (grade == 4)
            {
                frBus += 0.233;
                frWalk += 0.377;
            }
            if (grade == 5)
            {
                frBus += 0.757;
                frWalk += 0.71;
            }
            if (grade == 6)
            {
                frBus += 1.662;
                frWalk += 1.663;
            }

            //Race
            if (race == 1)
            {
                frBus -= 1.145;
                frWalk += 0.052;
            }

            frBus = Math.Exp(frBus);
            frWalk = Math.Exp(frWalk);

            probBusFr = frBus / (1 + frBus + frWalk);
            probWalkFr = frWalk / (1 + frBus + frWalk);
            probAutoFr = 1 / (1 + frBus + frWalk);

            double i = 0;
            i = rand.NextDouble();
            if ((i <= probBusFr))
            {
                //School Bus
                return 1;
            }
            else if ((i <= (probBusFr + probWalkFr)))
            {
                //Walk or Bike
                return 2;
            }
            else
            {
                //car
                return 3;
            }
        }

        private void btnSummary_Click(object sender, EventArgs e)
        {
            //Summary Functionality
            double tmpWalk = 0, tmpBus = 0, tmpAuto = 0;
            double tmpDistWalk = 0, tmpDistBus = 0, tmpDistAuto = 0;
            double emiVOC = 0, emiCO = 0, emiCO2 = 0, emiNOx = 0, emiPM = 0;
            string strConn = "";
            string temp = "", temp2 = "";

            //Total No. of Kids & Distance
            if (rs.State == 1) rs.Close();
            strConn = "Select ToMode, COUNT(ToMode), SUM(Distance) from ChildTravel GROUP BY ToMode";
            rs.Open(strConn, con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
            if (!rs.BOF) rs.MoveFirst();
            while (!rs.EOF)
            {
                if (Convert.ToInt32(rs.Fields[0].Value) == 1) { tmpBus = Convert.ToDouble(rs.Fields[1].Value); tmpDistBus = Convert.ToDouble(rs.Fields[2].Value); }
                if (Convert.ToInt32(rs.Fields[0].Value) == 2) { tmpWalk = Convert.ToDouble(rs.Fields[1].Value); tmpDistWalk = Convert.ToDouble(rs.Fields[2].Value); }
                if (Convert.ToInt32(rs.Fields[0].Value) == 3) { tmpAuto = Convert.ToDouble(rs.Fields[1].Value); tmpDistAuto = Convert.ToDouble(rs.Fields[2].Value); }
                rs.MoveNext();
            }

            if (rs2.State == 1) rs2.Close();
            strConn = "Select FrMode, COUNT(FrMode), SUM(Distance) from ChildTravel GROUP BY FrMode";
            rs2.Open(strConn, con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
            if (!rs2.BOF) rs2.MoveFirst();
            while (!rs2.EOF)
            {
                if (Convert.ToInt32(rs2.Fields[0].Value) == 1) { tmpBus += Convert.ToDouble(rs2.Fields[1].Value); tmpDistBus += Convert.ToDouble(rs2.Fields[2].Value); }
                if (Convert.ToInt32(rs2.Fields[0].Value) == 2) { tmpWalk += Convert.ToDouble(rs2.Fields[1].Value); tmpDistWalk += Convert.ToDouble(rs2.Fields[2].Value); }
                if (Convert.ToInt32(rs2.Fields[0].Value) == 3) { tmpAuto += Convert.ToDouble(rs2.Fields[1].Value); tmpDistAuto += Convert.ToDouble(rs2.Fields[2].Value); }
                rs2.MoveNext();
            }

            if (rs.State == 1) rs.Close();
            if (rs2.State == 1) rs2.Close();

            temp = "School Transportation Study Summary\r\n";
            temp += " -------------------------------------------------------------------------------------------------------\r\n";
            temp += "|       ||  No. of Kids (per day)   ||        %Travel Mode      ||      Kid Miles Traveled (per day)    |\r\n";
            temp += "|       ++--------------------------++--------+--------+--------++------------+------------+------------|\r\n";
            temp += "|       ||   Bus  |  Walk  |  Auto  ||   Bus  |  Walk  |  Auto  ||     Bus    |    Walk    |    Auto    |\r\n";
            temp += "|-------++--------+--------+--------++--------+--------+--------++------------+------------+------------|\r\n";
            temp += String.Format("| Whole || {0,6} | {1,6} | {2,6} || {3,6:N2} | {4,6:N2} | {5,6:N2} || {6,10:N2} | {7,10:N2} | {8,10:N2} |\r\n",
                tmpBus, tmpWalk, tmpAuto, (tmpBus / recCount * 50), (tmpWalk / recCount * 50), (tmpAuto / recCount * 50), tmpDistBus, tmpDistWalk, tmpDistAuto);
            temp += " -------++--------+--------+--------++--------+--------+--------++------------+------------+------------\r\n";
            temp += "|                                                                                                       |\r\n";
            temp += "| Categorized by School Type                                                                            |\r\n";

            //By School Type
            strConn = "SELECT Type, ToMode, COUNT(Distance) AS KidsCount, SUM(Distance) AS DistSum " 
                    + "FROM Children AS C, ChildTravel AS CT, School AS S " 
                    + "WHERE SID = S.ID AND C.ID = CT.ID " 
                    + "GROUP BY Type, ToMode";
            rs.Open(strConn, con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
            if (!rs.BOF) rs.MoveFirst();
            while (!rs.EOF)
            {
                if (Convert.ToInt32(rs.Fields[0].Value) == 1 && Convert.ToInt32(rs.Fields[1].Value) == 1) { tmpBus = Convert.ToDouble(rs.Fields[2].Value); tmpDistBus = Convert.ToDouble(rs.Fields[3].Value); }
                if (Convert.ToInt32(rs.Fields[0].Value) == 1 && Convert.ToInt32(rs.Fields[1].Value) == 2) { tmpWalk = Convert.ToDouble(rs.Fields[2].Value); tmpDistWalk = Convert.ToDouble(rs.Fields[3].Value); }
                if (Convert.ToInt32(rs.Fields[0].Value) == 1 && Convert.ToInt32(rs.Fields[1].Value) == 3) { tmpAuto = Convert.ToDouble(rs.Fields[2].Value); tmpDistAuto = Convert.ToDouble(rs.Fields[3].Value); }
                rs.MoveNext();
            }

            strConn = "SELECT Type, FrMode, COUNT(Distance) AS KidsCount, SUM(Distance) AS DistSum " 
                    + "FROM Children AS C, ChildTravel AS CT, School AS S " 
                    + "WHERE SID = S.ID AND C.ID = CT.ID " 
                    + "GROUP BY Type, FrMode";
            rs2.Open(strConn, con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic, -1);
            if (!rs2.BOF) rs.MoveFirst();
            while (!rs2.EOF)
            {
                if (Convert.ToInt32(rs2.Fields[0].Value) == 1 && Convert.ToInt32(rs2.Fields[1].Value) == 1) { tmpBus += Convert.ToDouble(rs2.Fields[2].Value); tmpDistBus += Convert.ToDouble(rs2.Fields[3].Value); }
                if (Convert.ToInt32(rs2.Fields[0].Value) == 1 && Convert.ToInt32(rs2.Fields[1].Value) == 2) { tmpWalk += Convert.ToDouble(rs2.Fields[2].Value); tmpDistWalk += Convert.ToDouble(rs2.Fields[3].Value); }
                if (Convert.ToInt32(rs2.Fields[0].Value) == 1 && Convert.ToInt32(rs2.Fields[1].Value) == 3) { tmpAuto += Convert.ToDouble(rs2.Fields[2].Value); tmpDistAuto += Convert.ToDouble(rs2.Fields[3].Value); }
                rs2.MoveNext();
            }
            temp += String.Format("|Magnet || {0,6} | {1,6} | {2,6} || {3,6:N2} | {4,6:N2} | {5,6:N2} || {6,10:N2} | {7,10:N2} | {8,10:N2} |\r\n",
                tmpBus, tmpWalk, tmpAuto, (tmpBus / (tmpBus + tmpWalk + tmpAuto) * 100), (tmpWalk / (tmpBus + tmpWalk + tmpAuto) * 100), (tmpAuto / (tmpBus + tmpWalk + tmpAuto) * 100), tmpDistBus, tmpDistWalk, tmpDistAuto);
            emiVOC = 0.7413 * tmpDistAuto * 1.10; emiCO = 15.5390 * tmpDistAuto * 1.10; emiCO2 = 443.5947 * tmpDistAuto * 1.10; emiNOx = 0.6972 * tmpDistAuto * 1.10; emiPM = 0.1346 * tmpDistAuto * 1.10;
            temp2 = "\r\n";
            temp2 += " ----------------------------------------------------------------------------------------------- \r\n";
            temp2 += "| Amount of Emissions (in grams per day)                                                        |\r\n";
            temp2 += " ----------------------------------------------------------------------------------------------- \r\n";
            temp2 += "| School Type | Mode |      VOC     |      CO      |      CO2     |      NOx     |      PM      |\r\n";
            temp2 += " -------------+------+--------------+--------------+--------------+--------------+-------------- \r\n";
            temp2 += String.Format("|  Magnet     | Auto | {0,12:N2} | {1,12:N2} | {2,12:N1} | {3,12:N2} | {4,12:N2} |\r\n",
                emiVOC, emiCO, emiCO2, emiNOx, emiPM);
            emiVOC = 0.618 * tmpDistAuto * 1.10; emiCO = 2.18 * tmpDistAuto * 1.10; emiCO2 = 1642.4 * tmpDistAuto * 1.10; emiNOx = 9.994 * tmpDistAuto * 1.10; emiPM = 0.6365 * tmpDistAuto * 1.10;
            temp2 += String.Format("|             | Bus  | {0,12:N2} | {1,12:N2} | {2,12:N1} | {3,12:N2} | {4,12:N2} |\r\n",
                emiVOC, emiCO, emiCO2, emiNOx, emiPM);
            temp2 += " -------------+------+--------------+--------------+--------------+--------------+-------------- \r\n";

            if (!rs.BOF) rs.MoveFirst();
            if (!rs2.BOF) rs2.MoveFirst();
            while (!rs.EOF)
            {
                if (Convert.ToInt32(rs.Fields[0].Value) == 2 && Convert.ToInt32(rs.Fields[1].Value) == 1) { tmpBus = Convert.ToDouble(rs.Fields[2].Value); tmpDistBus = Convert.ToDouble(rs.Fields[3].Value); }
                if (Convert.ToInt32(rs.Fields[0].Value) == 2 && Convert.ToInt32(rs.Fields[1].Value) == 2) { tmpWalk = Convert.ToDouble(rs.Fields[2].Value); tmpDistWalk = Convert.ToDouble(rs.Fields[3].Value); }
                if (Convert.ToInt32(rs.Fields[0].Value) == 2 && Convert.ToInt32(rs.Fields[1].Value) == 3) { tmpAuto = Convert.ToDouble(rs.Fields[2].Value); tmpDistAuto = Convert.ToDouble(rs.Fields[3].Value); }
                rs.MoveNext();
            }
            while (!rs2.EOF)
            {
                if (Convert.ToInt32(rs2.Fields[0].Value) == 2 && Convert.ToInt32(rs2.Fields[1].Value) == 1) { tmpBus += Convert.ToDouble(rs2.Fields[2].Value); tmpDistBus += Convert.ToDouble(rs2.Fields[3].Value); }
                if (Convert.ToInt32(rs2.Fields[0].Value) == 2 && Convert.ToInt32(rs2.Fields[1].Value) == 2) { tmpWalk += Convert.ToDouble(rs2.Fields[2].Value); tmpDistWalk += Convert.ToDouble(rs2.Fields[3].Value); }
                if (Convert.ToInt32(rs2.Fields[0].Value) == 2 && Convert.ToInt32(rs2.Fields[1].Value) == 3) { tmpAuto += Convert.ToDouble(rs2.Fields[2].Value); tmpDistAuto += Convert.ToDouble(rs2.Fields[3].Value); }
                rs2.MoveNext();
            }
            temp += String.Format("|NHood  || {0,6} | {1,6} | {2,6} || {3,6:N2} | {4,6:N2} | {5,6:N2} || {6,10:N2} | {7,10:N2} | {8,10:N2} |\r\n",
                tmpBus, tmpWalk, tmpAuto, (tmpBus / (tmpBus + tmpWalk + tmpAuto) * 100), (tmpWalk / (tmpBus + tmpWalk + tmpAuto) * 100), (tmpAuto / (tmpBus + tmpWalk + tmpAuto) * 100), tmpDistBus, tmpDistWalk, tmpDistAuto);
            temp += " -------++--------+--------+--------++--------+--------+--------++------------+------------+------------\r\n";
            if (rs.State == 1) rs.Close();
            if (rs2.State == 1) rs2.Close();
            emiVOC = 0.7413 * tmpDistAuto * 0.75; emiCO = 15.5390 * tmpDistAuto * 0.75; emiCO2 = 443.5947 * tmpDistAuto * 0.75; emiNOx = 0.6972 * tmpDistAuto * 0.75; emiPM = 0.1346 * tmpDistAuto * 0.75;
            temp2 += String.Format("|  NHood      | Auto | {0,12:N2} | {1,12:N2} | {2,12:N1} | {3,12:N2} | {4,12:N2} |\r\n",
                emiVOC, emiCO, emiCO2, emiNOx, emiPM);
            emiVOC = 0.6180 * tmpDistAuto * 0.75; emiCO = 2.18 * tmpDistAuto * 0.75; emiCO2 = 1642.4 * tmpDistAuto * 0.75; emiNOx = 9.994 * tmpDistAuto * 0.75; emiPM = 0.6365 * tmpDistAuto * 0.75;
            temp2 += String.Format("|             | Bus  | {0,12:N2} | {1,12:N2} | {2,12:N1} | {3,12:N2} | {4,12:N2} |\r\n",
                emiVOC, emiCO, emiCO2, emiNOx, emiPM);
            temp2 += " -------------+------+--------------+--------------+--------------+--------------+-------------- \r\n";
            txtSummary.Text = temp + temp2; ;
            btnPrint.Enabled = true;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //Printing Functionality
            DialogResult result = printDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                printDocument.Print();
            }
        }

        private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            System.Drawing.Font printFont = txtSummary.Font;
            e.Graphics.DrawString(txtSummary.Text, printFont, System.Drawing.Brushes.Black, 50, 50);
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtSummary.Text = "";
            btnPrint.Enabled = false;
        }



    }
}
